﻿// DigitalDesign
// Project: InformationBoard
// Developer: Bobylev Dmitry
// Created: 13.11.2011
// Last change: 26.11.2011
// Description: methods of video page.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using GUIAdmin.VideoServiceReference;
using GUIAdmin.AdminServiceReference;
using GUIAdmin.ScheduleServiceReference;
using System.Collections.Generic;
using System.ComponentModel;

namespace GUIAdmin.Views
{
    public partial class Video : Page
    {
        private VideoServiceClient videoClient = new VideoServiceClient();
        private AdminServiceClient adminClient = new AdminServiceClient();
        private ScheduleServiceClient scheduleClient = new ScheduleServiceClient();
        private List<InformationAboutVideo> videoList;

        public Video()
        {
            InitializeComponent();

            videoClient.GetPlaylistCompleted +=
                new EventHandler<GetPlaylistCompletedEventArgs>(videoClient_GetPlaylistCompleted);
            scheduleClient.GetDaysOfWeekCompleted +=
                new EventHandler<GetDaysOfWeekCompletedEventArgs>(scheduleClient_GetDaysOfWeekCompleted);
            adminClient.GetListOfClientsCompleted +=
                new EventHandler<GetListOfClientsCompletedEventArgs>(adminClient_GetListOfClientsCompleted);
            videoClient.GetListOfVideoCompleted +=
                new EventHandler<GetListOfVideoCompletedEventArgs>(videoClient_GetListOfVideoCompleted);
        }

        // Выполняется, когда пользователь переходит на эту страницу.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ObjectVisible();
            adminClient.GetListOfClientsAsync();
        }

        #region ServiceReferences EventHandler

        private void adminClient_GetListOfClientsCompleted(object sender, GetListOfClientsCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
                ClientList.ItemsSource = e.Result;
        }

        private void videoClient_GetPlaylistCompleted(object sender, GetPlaylistCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                PlaylistGrid.ItemsSource = e.Result[0].Collection;
            }
        }

        private void scheduleClient_GetDaysOfWeekCompleted(object sender, GetDaysOfWeekCompletedEventArgs e)
        {
            if (e.Result.IsGood)
            {
                DayComboBox.ItemsSource = e.Result.Data;
                PlaylistDaysOfWeek.ItemsSource = e.Result.Data;
            }
        }

        private void videoClient_GetListOfVideoCompleted(object sender, GetListOfVideoCompletedEventArgs e)
        {
            try
            {
                if (e.Result.FileName.Count > 0)
                {
                    if (e.Result.VideoID.Count == e.Result.FileName.Count &&
                        e.Result.VideoID.Count == e.Result.Description.Count)
                    {
                        videoList = new List<InformationAboutVideo>();
                        for (int i = 0; i < e.Result.VideoID.Count; i++)
                        {
                            videoList.Add(new InformationAboutVideo
                                (e.Result.VideoID[i], e.Result.FileName[i], e.Result.Description[i]));
                        }
                        if (videoList.Count != 0)
                        {
                            if (ChangePlaylist.Visibility == Visibility.Visible)
                                VideoCombobox.ItemsSource = videoList;
                            else
                                VideoGrid.ItemsSource = videoList;
                        }
                    }
                }
            }
            catch 
            {
                MessageBox.Show("Video service error: Data base does not contain any video-files.");
            }
        }        

        #endregion

        #region Button EventHandler

        //обновление таблицы с плейлистами при изменении дня недели
        private void PlaylistDaysOfWeek_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshPlaylistGrid();
        }

        //обновление таблицы с плейлистами при выборе другого клиента
        private void ClientList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (PlaylistGrid.Visibility == Visibility.Visible)
                RefreshPlaylistGrid();
        }

        //создание плейлиста
        private void CreatePlaylist_Click(object sender, RoutedEventArgs e)
        {
            DateTime timeStart, timeFinish;
            string stringTimeStart = TimeStartHours.Text + ":" + TimeStartMinutes.Text + ":00";
            string stringTimeFinish = TimeFinishHours.Text + ":" + TimeFinishMinutes.Text + ":00";

            if (DateTime.TryParse(stringTimeStart, out timeStart) && DateTime.TryParse(stringTimeFinish, out timeFinish)
                && ClientList.SelectedIndex != -1 && DayComboBox.SelectedIndex != -1 && 
                VideoCombobox.SelectedIndex != -1)
            {
                videoClient.AddPlaylistAsync(timeStart, timeFinish, ((InformationAboutVideo)(VideoCombobox.SelectedItem)).id,
                        ((ClientNotEF)(ClientList.SelectedItem)).Id, ((wDayOfWeek)(DayComboBox.SelectedItem)).Id);
            }
        }

        //показ панели для добавления плейлиста
        private void ShowAddPlaylist_Click(object sender, RoutedEventArgs e)
        {
            ObjectVisible(ChangePlaylist);
            videoClient.GetListOfVideoAsync(); 
            scheduleClient.GetDaysOfWeekAsync();
        }

        //удаление плейлиста
        private void delete_Click(object sender, RoutedEventArgs e)
        {
            if (PlaylistGrid.SelectedIndex != -1)
                videoClient.DeletePlaylistAsync(((PlaylistElement)PlaylistGrid.SelectedItem).ID);
        }

        //удаление видео
        private void VideoDelete_Click(object sender, RoutedEventArgs e)
        {
        }

        //показ списка доступных видео
        private void ShowVideoList_Click(object sender, RoutedEventArgs e)
        {
            ObjectVisible(VideoList);
            videoClient.GetListOfVideoAsync();
        }

        //добавление видео
        private void AddVideo_Click(object sender, RoutedEventArgs e)
        {
            if (FileName.Text != "" && Description.Text != "")
                videoClient.AddVideoAsync(FileName.Text, Description.Text);
        }
        
        #endregion

        #region Other methods
        
        //обновление таблицы с плейлистами
        private void RefreshPlaylistGrid()
        {
            if (ClientList.SelectedIndex != -1 && PlaylistDaysOfWeek.SelectedIndex != -1)
            {
                videoClient.GetPlaylistAsync(2, (PlaylistDaysOfWeek.SelectedItem as wDayOfWeek).Name);
            }
        }

        //делается видимым необходимый объект
        private void ObjectVisible(UIElement objectViisible = null)
        {
            VideoList.Visibility = Visibility.Collapsed;
            ChangePlaylist.Visibility = Visibility.Collapsed;
            Playlist.Visibility = Visibility.Collapsed;
            if (objectViisible != null)
                objectViisible.Visibility = Visibility.Visible;
        }

        //показ таблицы с плейлистами
        private void ShowPlaylist_Click(object sender, RoutedEventArgs e)
        {
            ObjectVisible(Playlist);
            if (PlaylistGrid.Visibility == Visibility.Visible)
                RefreshPlaylistGrid(); 
            scheduleClient.GetDaysOfWeekAsync();
        }

        #endregion
    }
}
